package cn.tedu.sp04.controller;

import cn.tedu.sp01.entity.Item;
import cn.tedu.sp01.entity.Order;
import cn.tedu.sp01.entity.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.web.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    //获取订单
    @GetMapping("/{orderId}")
    public JsonResult<Order> getOrder(
            @PathVariable String orderId) {
        Order order = orderService.getOrder(orderId);
        return JsonResult.ok().data(order);
    }

    //增加订单
    @GetMapping("/add")
    public JsonResult<?> addOrder() {
        Order order = new Order();
        order.setId("ksdhisg232");
        order.setUser(new User(5, "VzhV", "Zw134661"));
        order.setItems(Arrays.asList(new Item[]{
                new Item(1, "袜子", 1),
                new Item(2, "帽子", 1),
                new Item(3, "椅子", 1),
                new Item(4, "毛绒玩具", 1),
        }));

        orderService.addOrder(order);
        return JsonResult.ok().msg("增加订单成功");
    }

    @GetMapping("/favicon.ico")
    public void ico() {}//处理图标的方法

}
